package com.zwh.service.impl;

import com.zwh.entity.Patient;
import com.zwh.entity.Question;
import com.zwh.entity.User;
import com.zwh.mapper.UserMapper;
import com.zwh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    //连接mapper层
    @Autowired
    private UserMapper userMapper;
    @Override
    public void regist(User user) {
        User byname = userMapper.findByname(user.getName());
        if (!ObjectUtils.isEmpty(byname)) throw new RuntimeException("当前用户已被注册");
        //不然就注册用户，把数据保存到数据库中
        userMapper.save(user);
    }

    @Override
    public User login(String name, String password) {
        User user = userMapper.findByname(name);
        if (ObjectUtils.isEmpty(user)) throw new RuntimeException("用户名为空");
        if (!user.getName().equals(name)) throw new RuntimeException("用户名输入错误");
        //比较密码，因为这里没有对密码进行加密，所以直接比较即可
        // 获取数据库中存储的密码,user.getPassword()
        //String storedPassword = user.getPassword();
        if (ObjectUtils.isEmpty(password)) throw new RuntimeException("密码为空");
        if (!user.getPassword().equals(password)) throw new RuntimeException("密码输入错误");

        return user;

    }

    @Override
    public void patientkeep(Patient patient) {
        userMapper.savepatient(patient);
    }

    @Override
    public List<Patient> patientlists() {
        return userMapper.lists();
    }

    @Override
    public void questionkeep(Question question) {
        userMapper.questionsave(question);
    }
}
